package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.dao.TPSignAcctInfoMapper;
import cc.rengu.igas.momp.common.entity.TPSignAcctInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Jinan Liu
 * @date 2020/3/26 00:21
 * @description 营销活动机构规则数据库操作接口实现
 */
public class TPSignAcctInfoMapperImpl implements TPSignAcctInfoMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.OLTP_POOL_NAME);

    /**
     * @param tpSignAcctInfo
     * @return java.util.List<cc.rengu.igas.momp.common.entity.TMMcMchntInfo>
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2021/5/14 11:24
     * @description 签约信息列表查询接口实现
     */
    @Override
    public List<TPSignAcctInfo> selectSignAcctByInstIdUserIdTokenTypeCertNo(TPSignAcctInfo tpSignAcctInfo) throws Exception {


        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "MompSelectSignAcctByInstIdUserIdTokenTypeCertNo");

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, tpSignAcctInfo, TPSignAcctInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据查询条件<{}>获取签约信息列表失败!", tpSignAcctInfo);
            return null;
        }

        return objectList.stream().map(item -> (TPSignAcctInfo) (item)).collect(Collectors.toList());
    }

    /**
     * 根据法人机构号签约标记类型签约账号查询签约信息
     *
     * @param tpSignAcctInfo
     * @return TPSignAcctInfo
     * @throws Exception
     * @author liujinan
     * @date 2021.05.14
     */
    @Override
    public TPSignAcctInfo selectSignAcctByInstIdTokenTypeAcctNo(TPSignAcctInfo tpSignAcctInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "MompSelectSignAcctByInstIdTokenTypeAcctNo");

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tpSignAcctInfo, tpSignAcctInfo);
        if (null == tpSignAcctInfo) {
            rglog.error("根据查询条件<{}>获取签约信息表失败!", tpSignAcctInfo);
            return null;
        }

        return tpSignAcctInfo;
    }
}
